var request = require('../../utils/request.js')
var app = getApp();
Page({
  data: {
    longitude: '',
    latitude: '',
    height:'',
    skey:'',
    parameter:"",//需要的参数
    markers: [
    ],
    controls: [{
      id: 1,
      iconPath: '/images/scan.png',
      position: {
        left: '',
        top: '',
        width: 93,
        height: 93
      },
      clickable: true
    },
    {
      id: 2,
      iconPath: '/images/center.png',
      position: {
        left: '',
        top: '',
        width: 40,
        height: 40
      },
    },
    {
      id: 3,
      iconPath: '/images/call.png',
      position: {
        left: '',
        top: '',
        width: 40,
        height: 40
      },
      clickable: true
    },{
      id: 4,
      iconPath: '/images/back.png',
      position: {
        left: '',
        top: '',
        width: 40,
        height: 40
      },
      clickable: true
    },
    {
      id: 5,
      iconPath: '/images/cou1.png',
      position: {
        left: '',
        top: '',
        width: 40,
        height: 40
      },
      clickable:true
    },
    {
      id: 6,
      iconPath: '/images/chargeicon.png',
      position: {
        left: '',
        top: '',
        width: 40,
        height: 40
      },
      clickable:true
    },
    {
      id: 7,
      iconPath: '/images/card.png',
      position: {
        left: '',
        top: '',
        width:'',
        height: ''
      },
      clickable:true
    }
  ]
  },
  center(e) {
    if (e.type === 'end') {
      var that=this
      that.mapCtx.getCenterLocation({
        success: function(res){
          that.setData({
            longitude:res.longitude,
            latitude:res.latitude
          })
          that.getMark({'lng':res.longitude,'lat':res.latitude})
        }
      })
    }
  },
golist:function(e){
 my.navigateTo({
      url:'list?lat='+this.data.latitude+'&lng='+this.data.longitude+'&skey='+""
    })
},
  search:function(e){
    my.navigateTo({
      url:'list?lat='+this.data.latitude+'&lng='+this.data.longitude+'&skey='+e.detail.value
    })
  },
  scan:function (e) {
    if (e.controlId==3) {
      my.showActionSheet({
      title: '客服电话',
      items: ['028-67875563'],
      cancelButtonText: '取消',
      success: (res) => {
        if (res.index==0) {
          my.makePhoneCall({ number: '028-67875563' });
        }
      },
    });
    } else if (e.controlId==4){
      this.getLocation()
    } else if (e.controlId==1) {
      var that=this
      my.scan({
        success: (res) => {
          if (res.code.indexOf("qr") != -1) {
            let str = res.code.substr(res.code.length - 8, 8);
            my.navigateTo({
              url: 'order?sn=' + str
            })
          }
          if (res.code.indexOf("recharge") != -1) {
            let str = res.code.substr(res.code.length - 8, 8);
            my.navigateTo({
              url: '/pages/scanRecharge/index?cardId=' + str
            })
          }
        }
      })
    }else if(e.controlId ==5){
      my.navigateTo({
        url:'/pages/myWallet/coupon'
      }
      );
    }else if(e.controlId ==6){
      my.navigateTo({
        url:'/pages/user/wallet'
      }
      );
    }else if(e.controlId ==7){
      my.navigateTo({
        url:'/pages/myWallet/renew'
      }
      );
    }
  },
  
  getindex(e){
    for (var i = this.data.markers.length - 1; i >= 0; i--) {
      if (this.data.markers[i]['id']==e.markerId) {
        return i
      }
    }
  },
  controltap(e) {
    var i=this.getindex(e)
    this.setData({
      detail:this.data.markers[i]
    })
    var that=this;
    // my.showActionSheet({
    //   title: '设备信息',
    //   items: [this.data.markers[i]['sn']],
    //   cancelButtonText: '取消',
    //   success: (res) => {
    //     if (res.index==0) {
    //       my.navigateTo({
    //         url:'order?sn='+that.data.markers[i]['sn']
    //       })
    //     }
    //    },
    //   });
      my.confirm({
        title: '设备编号'+that.data.markers[i]['sn'],
        content: that.data.markers[i]['address'],
        confirmButtonText: '马上充电',
        cancelButtonText: '暂不需要',
        success: (result) => {
          if(result.confirm === true){
            my.navigateTo({
              url:'order?sn='+that.data.markers[i]['sn']
            })
          }
        },
      });
  },
    //切换页面
  callouttap:function(e){
  my.navigateTo({
  url:"order?sn="+"123456"
 })
},
  enter(){
    my.navigateTo({
      url:'order?sn='+this.data.detail['sn']
    })
  },
  getMark:function(data){
    var that=this;
    var success=function(data){
      that.setData({
        markers:data
      })
    }
    request.wxrequest('/Map/getFacility',data,success,that)
  },
  getLocation:function(){
    my.getLocation({
      type:1,
      success: (res)=> {
        my.setStorageSync({
          key: 'locationlist',
          data: {
            province: res.province,
            area_id: res.districtAdcode,
            city: res.city,
          }
        });
        this.getMark({'lng':res['longitude'],'lat':res['latitude']})
        this.setData({
            longitude:res['longitude'],
            latitude:res['latitude']
        })
      },
      fail: function(res){
        // if (res.errMsg =='getLocation:fail auth deny') {
        //   wx.showModal({
        //     title: '请授权',
        //     content: '未授权部分功能将无法使用',
        //     showCancel: false,
        //     success: function(res) {
        //       wx.openSetting({
        //         success:function(res){
        //           that.getLocation()
        //         }
        //       })
        //     }
        //   }) 
        // }
      }
    })
  },
  clear:function(){
    this.setData({
      detail:''
    })
  },
  onLoad:function(){
    this.getLocation()
    var that = this
    my.getSystemInfo({
      success: function(res) {
        that.data.controls[0]['position']['left'] = res.windowWidth/2-46
        that.data.controls[0]['position']['top'] = res.windowHeight*0.65
        that.data.controls[1]['position']['left'] = res.windowWidth/2-20
        that.data.controls[1]['position']['top'] = res.windowHeight/2-20
        that.data.controls[2]['position']['left'] = res.windowWidth-55
        that.data.controls[2]['position']['top'] = res.windowHeight-100
        that.data.controls[3]['position']['left'] = 15
        that.data.controls[3]['position']['top'] = res.windowHeight-100
        that.data.controls[4]['position']['left'] = res.windowWidth-55
        that.data.controls[4]['position']['top'] = res.windowHeight-200
        that.data.controls[5]['position']['left'] = 15
        that.data.controls[5]['position']['top'] = res.windowHeight-200
        that.data.controls[6]['position']['left'] = 0
        that.data.controls[6]['position']['top'] =15
        that.data.controls[6]['position']['width'] =res.windowWidth
        that.data.controls[6]['position']['height'] =res.windowHeight*0.07
        that.setData({
          height:res.windowHeight-43+'px',
          controls:that.data.controls
        })
      }
    })
  },
  onShow:function(){
    my.getNetworkType({
      success: (res) => {
        if(!res.networkAvailable||res.networkType=='UNKNOWN'){
          my.showToast({
            content: '网络状态不佳，请检查网络',
            type:'fail',
            duration: 2000
          })
        }
      }
    })
  },
  onReady() {
    this.mapCtx = my.createMapContext('map');
    this.onLoad();
  },
})